package com.whu.waterworks.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.whu.waterworks.dto.StationAllDataDto;
import com.whu.waterworks.dto.StationDataDto;
import com.whu.waterworks.model.Realtimedata;
import com.whu.waterworks.model.Stationnumposition;
import com.whu.waterworks.service.DataService;
import com.whu.waterworks.service.StationnumpositionService;
import com.whu.waterworks.util.CodeTransformationUtil;

/**
 * 
 * @author wangying
 *
 */
public class GetDataAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 从前台JS传入的四个值，可以为空
	 */
	private String id;
	private String type;//1，流量等；2压力
	private String beginTime;
	private String endTime;
	//返回的值
	private List<StationDataDto> realtimedatas;
	private List<StationAllDataDto> allDataDtos;
	
	private DataService dataService;
	private StationnumpositionService stationnumpositionService;
	
	public List<Stationnumposition> getAllStationByWaterPlantCode()
	{
		List<Stationnumposition> stationnumpositions = stationnumpositionService.findAll(id);
		if(stationnumpositions!=null && stationnumpositions.size()>0)
		{
			return stationnumpositions;
		}
		return null;
	}
	
	/**
	 * 获取指定时间段历史数据
	 * @author wangying
	 * @return
	 */
	public String getHistoryDataByIdType()
	{
		System.out.println("GetDataAction getHistoryDataByIdType id="+id+"  type="+type+"  beginTime="+beginTime+"  endTime="+endTime);
		if(id!=null && type!=null)
		{
			try {
				realtimedatas = getRealTimeWaterData(id, type,beginTime,endTime);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			
		}
		return SUCCESS;
	}
	
	/**
	 * 获取特定时间段指定类型的值
	 * @author wangying
	 * @return
	 */
	public List<StationDataDto> getRealTimeWaterData(String id, String type,String beginTime,String endTime)
	{
		Stationnumposition station = stationnumpositionService.findStationById(id);
		System.out.println("GetDataAction station.name="+station.getPositionname());
		List<StationDataDto> stationWaterDataDtos = new ArrayList<>();
		List<Realtimedata> realtimedatas = dataService.getDataByTwoTime(id, type,beginTime,endTime);
		System.out.println("GetDataAction realtimedatas.size()="+realtimedatas.size());
		for(Realtimedata realtimedata:realtimedatas)
		{
			StationDataDto stationDataDto = new StationDataDto();
			stationDataDto.setStationnum(station.getPositionname());
			stationDataDto.setTime(realtimedata.getId().getTime());
			stationDataDto.setType(realtimedata.getId().getType());
			stationDataDto.setValue(realtimedata.getValue());
			stationWaterDataDtos.add(stationDataDto);
		}
		
		return stationWaterDataDtos;		
	}
	/**
	 * 最新一条实时信息
	 * @author wangying
	 * @return
	 */
	public String getAllRealData()
	{
		allDataDtos = new ArrayList<>();
		
		//所有的站点
		List<Stationnumposition> stationnumpositions = stationnumpositionService.findAll(CodeTransformationUtil.getWaterplantCode(id));
		//每个站点循环
		for(Stationnumposition stationnumposition:stationnumpositions)
		{
			//System.out.println("GetDataAction stationnumposition="+stationnumposition.getStationnum());
			StationAllDataDto stationAllDataDto = new StationAllDataDto();
			try {
				List<Realtimedata> realtimedatas = dataService.getAllRealTimeDataById(stationnumposition.getStationnum());
				if(realtimedatas!=null && realtimedatas.size()>0)
				{
					//每个站点的所有类型数据
					for(Realtimedata realtimedata:realtimedatas)
					{
						stationAllDataDto.setStationnum(stationnumposition.getPositionname());
						if(realtimedata.getId().getType().contains("流量"))
						{
							stationAllDataDto.setFlowValue(realtimedata.getValue());
						}
						else if(realtimedata.getId().getType().contains("压力"))
						{
							stationAllDataDto.setPressureValue(realtimedata.getValue());
						}
						else if(realtimedata.getId().getType().contains("浊度"))
						{
							stationAllDataDto.setTurbidityValue(realtimedata.getValue());
						}
						else if(realtimedata.getId().getType().contains("余氯"))
						{
							stationAllDataDto.setChlorineValue(realtimedata.getValue());
						}
					}
					allDataDtos.add(stationAllDataDto);
				}

			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
	

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public DataService getDataService() {
		return dataService;
	}

	public void setDataService(DataService dataService) {
		this.dataService = dataService;
	}
	public List<StationDataDto> getRealtimedatas() {
		return realtimedatas;
	}
	public void setRealtimedatas(List<StationDataDto> realtimedatas) {
		this.realtimedatas = realtimedatas;
	}

	public StationnumpositionService getStationnumpositionService() {
		return stationnumpositionService;
	}

	public void setStationnumpositionService(StationnumpositionService stationnumpositionService) {
		this.stationnumpositionService = stationnumpositionService;
	}

	public List<StationAllDataDto> getAllDataDtos() {
		return allDataDtos;
	}

	public void setAllDataDtos(List<StationAllDataDto> allDataDtos) {
		this.allDataDtos = allDataDtos;
	}

}
